import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import { resolve } from 'node:path';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
// import dotenv from 'dotenv';
// import.meta.env.VITE_API_URL;
// dotenv.config();

const pathResolve = (dir: string): any => {
	return resolve(__dirname, '.', dir);
};
const alias: Record<string, string> = {
	'@/': pathResolve('./src/'),
};

export default defineConfig({
	plugins: [
		uni(),
		AutoImport({
			// imports:['vue','pinia','uniapp'],
			imports: ['vue', 'pinia'],
			dts: true,
			dirs: ['./src/stores'],
			eslintrc: {
				enabled: true,
			},
			vueTemplate: true,
		}),
		Components({
			dts: 'src/components.d.ts',
		}),
	],

	resolve: { alias },

	/*构建选项*/
	build: {
		outDir: 'dist',
		assetsDir: 'assets',
		cssCodeSplit: true,
		sourcemap: false,
		minify: 'terser',
		terserOptions: {
			compress: {
				// drop_console: true,
				// drop_debugger:true
			},
		},
		rollupOptions: {
			output: {
				manualChunks: (id) => {
					if (id.includes('node_modules')) {
						return 'vendor';
					}
				},
			},
		},
	},

	esbuild: {
		// 打包去除 console.log 和 debugger
		// drop: env.VITE_DROP_CONSOLE && command === 'build' ? ["console", "debugger"] : []
	},

	/*开发服务器选项*/
	server: {
		// 端口
		port: 3000,
		// open: env.VITE_OPEN,
		// 代理配置
		proxy: {
			'/api': {
				target: process.env.VITE_API_URL,
				changeOrigin: true,
			},
		},
		cors: true,
	},
});
